package com.ITArmy.service.impl;

import com.ITArmy.beans.*;
import com.ITArmy.beans.Shows.CommentMessage;
import com.ITArmy.beans.Shows.QuestionShow;
import com.ITArmy.beans.Shows.ReadBlog;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.dao.*;
import com.ITArmy.service.QuestionService;
import com.ITArmy.service.ReplyService;
import com.ITArmy.service.TypeService;
import com.ITArmy.util.CheckMessage;
import com.ITArmy.util.GetUserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    GetUserSession getUserSession = new GetUserSession();
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    QuestionService questionService;
    @Autowired
    TypeService typeService;
    @Autowired
    PraiseMapper praiseMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    FocusMapper focusMapper;
    @Autowired
    ReplyService replyService;
    @Autowired
    ReplyMapper replyMapper;

    /**
     * 遍历所有问题
     */
    @Override
    public List<Question> findAllQuestion() {
            return questionMapper.findAllQuestion();
    }

    /**
     *通过问题id查找信息
     */
    @Override
    public QuestionShow findQuestionById(HttpServletRequest request,Long qid) {
        QuestionShow questionShow=new QuestionShow();
        Question question= questionMapper.selectByPrimaryKey(qid);
        //存问题信息
        questionShow.setQuestion(question);
        //存提问人信息
        questionShow.setUser(userMapper.selectByPrimaryKey(questionShow.getQuestion().getaUId()));
        //评论量
        List<Comment> comments = commentMapper.findCommentByQuestionId(qid);
        questionShow.setCommentNumber(comments.size());
        //显示是否关注过作者
        questionShow.setLikeUser(false);
        if (focusMapper.checkIsFocus(question.getaUId(), getUserSession.getUser(request).getuId()) != null) {
            System.out.println("关注过啊");
            questionShow.setLikeUser(true);
        } else {
            System.out.println("没有关注过...");
        }
        return questionShow;
    }

    @Override
    public ServerResponse findQuestionByIdByBack(long id) {
        Question question=questionMapper.selectByPrimaryKey(id);
        if (question!=null){
            return ServerResponse.createBySuccess(question);
        }
        return ServerResponse.createByErrorMesage("内容为空");
    }

    /**
     * 添加问题
     */
    @Override
    public ServerResponse addQuestion(Question question) {
        int resultCount = questionMapper.insertSelective(question);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "添加成功", "添加失败");
    }

    /**
     * 修改问题信息
     */
    @Override
    public ServerResponse updateQuestion(Question question) {
        int resultCount = questionMapper.updateByPrimaryKeySelective(question);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "修改成功", "修改失败");

    }

    /**
     *根据问题标题模糊搜索问题信息
     */
    @Override
    public ServerResponse findQuestionByTitle(String title) {
        List<Question> question = questionMapper.findQuestionByTitle(title);
        if (question != null) {
            return ServerResponse.createBySuccess(question);
        } else {
            return ServerResponse.createByErrorMesage("无相关信息！");
        }
    }

    /**
     *根据类型查找问题信息
     */
    @Override
    public ServerResponse findQuestionByType(Integer tid) {
        List<Question> question = questionMapper.findQuestionByType(tid);
        if (question != null) {
            return ServerResponse.createBySuccess(question);
        } else {
            return ServerResponse.createByErrorMesage("无相关信息！");
        }
    }

    /**
     * 删除问题
     */
    @Override
    public ServerResponse deleteQuestionById(Long id) {
        int resultCount = questionMapper.deleteByPrimaryKey(id);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "删除成功", "删除失败");
    }

    /**
     * 批量删除
     */
    @Override
    public ServerResponse deleteManyByIds(String[] ids) {
        int resultCount = questionMapper.deleteManyByIds(ids);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "删除成功", "删除失败");
    }

    /**
     * 进入主页
     * (前台)进行展示所有问题，封装了提问的人的信息，和问题的全部信息
     *
     * */
    @Override
    public void goIndex(Model model) {
        ServerResponse typeServerResponse = typeService.typeList();
        List<Question> questions = questionMapper.findAllQuestion();
        List<QuestionShow> questionShows = new ArrayList<>();
        for (int i = 0; i < questions.size(); i++) {
            QuestionShow shows=new QuestionShow();
            //用户信息
            shows.setUser(userMapper.selectByPrimaryKey(questions.get(i).getaUId()));
            //问题信息
            shows.setQuestion(questions.get(i));
            //回答数量
            shows.setCommentNumber(commentMapper.findCommentByQuestionId(questions.get(i).getqId()).size());
            questionShows.add(shows);
        }
        model.addAttribute("typeList", typeServerResponse.getData());
        model.addAttribute("questionList", questionShows);
    }

    /**
     * (前台)发表问题
     */
    @Override
    public ServerResponse publishQuestion(HttpServletRequest request, Question question) {
        User user = getUserSession.getUser(request);
        question.setaUId(user.getuId());
        question.setqTime(new Date());
        if (question.getqReward()<=question.getaUId()){
            int resultCount = questionMapper.insertSelective(question);
            CheckMessage check = new CheckMessage();
            return check.getMessage(resultCount, "发表成功", "发表失败");
        }else {
            return ServerResponse.createByErrorMesage("余额不足,请减少悬赏金额");
        }
    }

    /**
     * （前台）采纳回答
     */
    @Override
    public ServerResponse adoptComment(HttpServletRequest request,long ruid, long qid,long cid) {
        int resultCount = questionMapper.solveQuestion(ruid,qid);
        if (resultCount==1){
            //获取赏金
            double reward=questionMapper.getReward(qid);
            //将提问者对应金币转入解决者账户中
            User user=getUserSession.getUser(request);
            userMapper.reduceGold(reward,user.getuId());
            userMapper.addGold(reward,ruid);
        }
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "采纳成功", "采纳失败");
    }

    /**
     * 获取关于回答的所有消息
     */
    @Override
    public List<CommentMessage> getCommentMessage(long qid) {
        //查询关于问题的所有回答信息
        List<Comment> commons = commentMapper.findCommentByQuestionId(qid);
        //创建集合储存封装的评论信息
        List<CommentMessage> commentMessages = new ArrayList<>();
        if (commons == null) {
            //如果这个问题没有回答，返回一个空的集合
            return commentMessages;
        }
        for (int i = 0; i < commons.size(); i++) {
            CommentMessage commentMessage = new CommentMessage();
            //将回答者储存到封装的对象中
            commentMessage.setUser(userMapper.selectByPrimaryKey(commons.get(i).getuId()));
            //将回答的具体信息储存到封装对象中
            commentMessage.setComment(commons.get(i));
            //将问题信息储存到封装对象中
            commentMessage.setQuestion(questionMapper.selectByPrimaryKey(commons.get(i).getqId()));
            //获取这条评论的所有回复
            commentMessage.setReplyMessage(replyService.getReplyMessageByCid(commons.get(i).getcId()));
            //获取回复数量
            commentMessage.setReplyCount(replyMapper.replycount(commons.get(i).getcId()));
            //将数据添加到集合中
            commentMessages.add(commentMessage);
        }
        return commentMessages;
    }
}
